package com.demo.commonsio;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

/**
 * 使用org.apache.commons.io.FileUtils读写文件
 * @author Administrator
 *
 */
public class FileReadAndWite {
	
	public static void main(String[] args) throws IOException {
		String fileName = FileReadAndWite.class.getClass().getResource("/").getFile()+"/nio-data.txt";
		
		/**
		 * 使用IO读取大文件，容易引起java.lang.OutOfMemoryError
		 */
		//String fileName = "F:/迅雷下载/CentOS-6.8-x86_64-bin-DVD1to2/CentOS-6.8-x86_64-bin-DVD1.iso";
		readFile(fileName);
		
		String fname = "temp.txt";
		for (int i = 0; i < 10; i++) {
			writeFile(fname, "line num"+(i+1)+"\t2344556\t 电视剧 、电影\n", true);
		}
		readFile(fname);
		FileUtils.deleteQuietly(new File(fname));
	}
	
	public static void readFile(String fileName) throws IOException{
		System.out.println(FileUtils.readFileToString(new File(fileName)));
	}
	
	public static void writeFile(String fileName, String data, boolean append) throws IOException{
		FileUtils.writeStringToFile(new File(fileName), data, append);
	}

}
